package com.lxinet.jeesns.modules.wechat.listener;

import com.blade.kit.json.JSONObject;
import com.lxinet.jeesns.modules.wechat.entity.WechatMeta;
import com.lxinet.jeesns.modules.wechat.service.IWechatMetaService;
import com.lxinet.jeesns.modules.wechat.service.WechatService;
import com.lxinet.jeesns.utils.JedisUtil;
import com.lxinet.jeesns.utils.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

@Component
public class WechatListener {
    @Resource
    private WechatService wechatService;
    @Resource
    private  IWechatMetaService wechatMetaService;
	private static final Logger LOGGER = LoggerFactory.getLogger(WechatListener.class);
    private Jedis jedis = JedisUtil.getInstance().getJedis();
	int playWeChat = 0;
	String uuid="";
	
	public void start(){
		new Thread(new Runnable() {
			@Override
			public void run() {
                while(true) {
                    uuid=jedis.get("tempuuid");
                    List<WechatMeta> list =  wechatMetaService.getAll();
                    for(int i = 0 ; i < list.size(); i++){
                        WechatMeta wechatMeta = list.get(i);
                        if (wechatMeta !=null&&null != wechatMeta.getDlcode()) {
                            LOGGER.info("进入消息监听模式 ...");
                            try{
                                wechatService.choiceSyncLine(wechatMeta);
                            }catch (Exception e){
                                LOGGER.info("同步异常");
                            }
                            int[] arr = wechatService.syncCheck(wechatMeta);
                            LOGGER.info("retcode={}, selector={}", arr[0], arr[1]);
                            if (arr[0] == 1100) {
                                LOGGER.info("你在手机上登出了微信，债见");
                                wechatMetaService.delete(wechatMeta.getId());
                                break;
                            }
                            if (arr[0] == 0) {
                                if (arr[1] == 2) {
                                    JSONObject data = wechatService.webwxsync(wechatMeta);
                                    wechatService.handleMsg(wechatMeta, data);
                                } else if (arr[1] == 6) {
                                    JSONObject data = wechatService.webwxsync(wechatMeta);
                                    wechatService.handleMsg(wechatMeta, data);
                                } else if (arr[1] == 7) {
                                    playWeChat += 1;
                                    LOGGER.info("你在手机上玩微信被我发现了 {} 次", playWeChat);
                                    wechatService.webwxsync(wechatMeta);
                                } else if (arr[1] == 3) {
                                    continue;
                                } else if (arr[1] == 0) {
                                    continue;
                                }
                            } else {
                                //
                            }
                        }
                        try {
                            LOGGER.info("等待2000ms...");
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
			}
		}, "wechat-listener-thread").start();
	}
	
}
